package com.example.retrofitdemo_618lw.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.retrofitdemo_618lw.R;
import com.example.retrofitdemo_618lw.bean.LoginBean;
import com.example.retrofitdemo_618lw.iface.LoginListener;
import com.example.retrofitdemo_618lw.model.LoginModel;

public class MainActivity extends AppCompatActivity {
    private EditText etuser,etpass;
    private Button login,signin;
    private String username="", password="", sessionID="";
    private Switch s;
    private SharedPreferences sp;


    private LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            sessionID = loginBean.getSessionid();
            Log.i("LoginActivity","--sessionID"+ sessionID);
            if (sessionID !=null){
                Toast.makeText(MainActivity.this,"Suceessed"+sessionID,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this,Main3Activity.class);
                startActivity(intent);
                finish();


            }else
                Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this, msg,Toast.LENGTH_SHORT).show();

        }
    };





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        initviews();
        sp = getSharedPreferences("login",MODE_PRIVATE);
        readSP();
        saveSP();

    }

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",etuser.getText().toString());
        editor.putBoolean("remember",s.isChecked());
        editor.commit();


    }

    private void readSP() {
        String name=sp.getString("username","");
        String pass=sp.getString("password","");
        boolean remember=sp.getBoolean("remember",false);
        s.setChecked(remember);
        if(remember){
            etuser.setText(name);
            etpass.setText(pass);
        }

    }


    private void initviews() {
        etpass=findViewById(R.id.etpass);
        etuser=findViewById(R.id.etname);
        login=findViewById(R.id.button);
        signin=findViewById(R.id.button);
        s=findViewById(R.id.switch);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = etuser.getText().toString();
                password = etpass.getText().toString();
                LoginModel loginModel = new LoginModel();
                LoginModel.getLoginResult(username,password,loginListener);

            }
        });
        signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,
                        Main2Activity.class);
            }

        });



    }



}
